package com.example.wendao.view.adapter;

import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wendao.common.PageUtils;
import com.example.wendao.model.entity.Fans;
import com.example.wendao.model.entity.User;
import com.example.wendao.view.adapterdataobserverproxy.AdapterDataObserverProxy;
import com.example.wendao.view.viewholder.FansViewHolder;
import com.example.wendao.view.viewholder.FooterViewHolder;

public class FansListAdapter extends PagedListAdapter<User, RecyclerView.ViewHolder> {


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        if (viewType == PageUtils.ITEM_TYPE_FOOTER){
            viewHolder = new FooterViewHolder(parent);
        } else {
            viewHolder = new FansViewHolder(parent);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        /*写监听*/
    }
    @Override
    public int getItemCount() {
        return super.getItemCount() + 1;
    }

    private User getUserItem(int position){
        return super.getItem(position);
    }

    @Override
    public int getItemViewType(int position) {
        int type;
        int itemCount = getItemCount() - 1;
        if (position == itemCount){
            type = PageUtils.ITEM_TYPE_FOOTER;
        } else {
            type = super.getItemViewType(position);
        }
        return type;
    }


    @Override
    public void registerAdapterDataObserver(@NonNull RecyclerView.AdapterDataObserver observer) {
        // 此处没有header，headerCount设置为0
        super.registerAdapterDataObserver(new AdapterDataObserverProxy(observer, 0));
    }

    private static DiffUtil.ItemCallback<User> DIFF_CALLBACK = new DiffUtil.ItemCallback<User>() {


        @Override
        public boolean areItemsTheSame(@NonNull User oldUser, @NonNull User newUser) {
            return oldUser.getUserId() == newUser.getUserId();
        }

        @Override
        public boolean areContentsTheSame(@NonNull User oldUser, @NonNull User newUser) {
            return oldUser.equals(newUser);
        }
    };
    public FansListAdapter() {
        super(DIFF_CALLBACK);
    }
}

